
package com.shop.cloud.mall.api.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.shop.cloud.common.core.constant.CacheConstants;
import com.shop.cloud.common.core.constant.CommonConstants;
import com.shop.cloud.common.core.util.R;
import com.shop.cloud.common.data.tenant.TenantContextHolder;
import com.shop.cloud.common.portal.constant.PortalConstants;
import com.shop.cloud.common.portal.util.ApiUtil;
import com.shop.cloud.mall.api.service.PageDeviseService;
import com.shop.cloud.mall.common.entity.PageDevise;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

/**
 * 页面设计表
 *
 * @author
 * @date 2020-09-22 13:38:09
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/pagedevise")
@Tag(description = "pagedevise", name = "页面设计API")
public class PageDeviseController {

    private final PageDeviseService pageDeviseService;
	private final RedisTemplate redisTemplate;

    /**
     * 页面设计表查询
     * @param pageDevise
     * @return R
     */
    @Operation(summary = "页面设计表查询")
    @GetMapping
    public R getById(HttpServletRequest request, PageDevise pageDevise) {
    	if(StrUtil.isBlank(pageDevise.getPageType())){
			return R.failed("pageType不能为空");
		}
		if(StrUtil.isBlank(pageDevise.getShopId())){
			return R.failed("shopId不能为空");
		}
		String clientType = ApiUtil.getClientType(request);
		pageDevise.setAppId("-1");
		if("-1".equals(pageDevise.getShopId())){
			if(PortalConstants.CLIENT_TYPE_H5_WX.equals(clientType)){//公众号h5端区分到appId
				String appId = ApiUtil.getAppId(request);
				pageDevise.setAppId(appId);
			}
			if(PortalConstants.CLIENT_TYPE_MA.equals(clientType)){//小程序端区分到appId
				String appId = ApiUtil.getAppId(request);
				pageDevise.setAppId(appId);
			}
			pageDevise.setClientType(clientType);
			pageDevise.setShopId("-1");
		}else{
			if(PortalConstants.CLIENT_TYPE_H5_PC.equals(clientType)){
				pageDevise.setClientType(clientType);
			}else{
				pageDevise.setClientType("-1");
			}
		}

    	//先取缓存
		String key = StrUtil.format("{}:{}:{}:{}:{}:{}",
				TenantContextHolder.getTenantId(),
				CacheConstants.MALL_PAGE_DEVISE_CACHE,
				pageDevise.getPageType(),
				pageDevise.getClientType(),
				pageDevise.getShopId(),
				pageDevise.getAppId());
		if(redisTemplate.hasKey(key)) {
			Object obj = redisTemplate.opsForValue().get(key);
			if(obj != null){
				String str = String.valueOf(obj);
				pageDevise = JSONUtil.toBean(str, PageDevise.class);
			}else{
				pageDevise = null;
			}
		}else {
			pageDevise.setEnable(CommonConstants.YES);
			pageDevise = pageDeviseService.getOne(Wrappers.query(pageDevise));
			//存入缓存
			redisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(pageDevise),5, TimeUnit.DAYS);
		}
		return R.ok(pageDevise);
    }

}
